using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Media.Imaging;
namespace MicroMsg.Plugin.FaceBook
{
	public class FaceBookMgr
	{
		private const string token_updateTime = "token_updateTime";
		private const string token_expires = "token_expires";
		private const string IsLastLoginAsFaceBook = "IsLastLoginAsFaceBook";
		private const string IsFacebookAccountChanged = "IsFacebookAccountChanged";
		public static int iTokenExpires = 0;
		public static string strUpdateTokenTime = "";
		private static Dictionary<ulong, MemoryStream> mapHeadImg = new Dictionary<ulong, MemoryStream>();
		private static List<ulong> listFaceBookID = new List<ulong>();
		public static string GetFaceBookShowName(FBContact fbcontact_0)
		{
			if (fbcontact_0 == null)
			{
				return "";
			}
			if (!string.IsNullOrEmpty(fbcontact_0.strFacebookName))
			{
				return fbcontact_0.strFacebookName;
			}
			if (!string.IsNullOrEmpty(fbcontact_0.strNickName))
			{
				return fbcontact_0.strNickName;
			}
			if (!string.IsNullOrEmpty(fbcontact_0.strUsrName))
			{
				return fbcontact_0.strUsrName;
			}
			return "";
		}
		public static Contact GetContactFromFB(FBContact fbContact)
		{
			if (!string.IsNullOrEmpty(fbContact.strUsrName))
			{
				Contact contact = StorageMgr.contact.method_0(fbContact.strUsrName);
				if (contact != null)
				{
					return contact;
				}
			}
			Contact contact2 = FBContactMgr.ConvertToContact(fbContact);
			contact2.nImgFlag = 3u;
			return contact2;
		}
		public static void DelaySaveTokenTime()
		{
			if (FaceBookMgr.iTokenExpires != 0 && !string.IsNullOrEmpty(FaceBookMgr.strUpdateTokenTime))
			{
				ConfigMgr.setString("token_updateTime", FaceBookMgr.strUpdateTokenTime);
				ConfigMgr.setInt("token_expires", FaceBookMgr.iTokenExpires);
				FaceBookMgr.iTokenExpires = 0;
				FaceBookMgr.strUpdateTokenTime = "";
			}
		}
		public static void UpdateTokenTime(string strExpires)
		{
			if (string.IsNullOrEmpty(strExpires))
			{
				return;
			}
			if (AccountMgr.hasValidAccount())
			{
				int value = int.Parse(strExpires);
				ConfigMgr.setString("token_updateTime", DateTime.get_Now().ToString());
				ConfigMgr.setInt("token_expires", value);
				return;
			}
			FaceBookMgr.iTokenExpires = int.Parse(strExpires);
			FaceBookMgr.strUpdateTokenTime = DateTime.get_Now().ToString();
		}
		public static void ForceTokenExpires()
		{
			ConfigMgr.setInt("token_expires", 0);
		}
		public static bool IsTokenExpires()
		{
			string text = DateTime.get_Now().AddDays(-1.0).ToString();
			text = ConfigMgr.getString("token_updateTime", text);
			DateTime dateTime;
			bool result;
			try
			{
				dateTime = Convert.ToDateTime(text);
				goto IL_4C;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
			IL_4C:
			int @int = ConfigMgr.getInt("token_expires", 0);
			if (dateTime > DateTime.get_Now())
			{
				return true;
			}
			DateTime dateTime2 = dateTime.AddSeconds((double)@int);
			return !(DateTime.get_Now() < dateTime2) || !(DateTime.get_Now() >= dateTime);
		}
		public static bool IsChina()
		{
			RegionInfo currentRegion = RegionInfo.get_CurrentRegion();
			return currentRegion.get_Name() == "CN";
		}
		public static void SetLastLoginAsFaceBook(bool isTrue)
		{
			GConfigMgr.setBool("IsLastLoginAsFaceBook", isTrue);
		}
		public static bool LastLoginAsFaceBook()
		{
			return GConfigMgr.getBool("IsLastLoginAsFaceBook", false);
		}
		public static BitmapImage getCache(ulong facebookID)
		{
			if (!FaceBookMgr.mapHeadImg.ContainsKey(facebookID))
			{
				return null;
			}
			BitmapImage bitmapImage;
			try
			{
				bitmapImage = new BitmapImage();
				bitmapImage.SetSource(FaceBookMgr.mapHeadImg.get_Item(facebookID));
			}
			catch (Exception ex)
			{
				ex.ToString();
				bitmapImage = null;
			}
			return bitmapImage;
		}
		private static MemoryStream getStream(Stream stream)
		{
			MemoryStream memoryStream = new MemoryStream();
			int num = 1024;
			byte[] array = new byte[1024];
			int num2;
			while ((num2 = stream.Read(array, 0, num)) > 0)
			{
				memoryStream.Write(array, 0, num2);
			}
			if (memoryStream.get_Length() > 0L)
			{
				memoryStream.Seek(0L, 0);
			}
			return memoryStream;
		}
		public static BitmapImage addCache(ulong facebookID, Stream stream)
		{
			if (stream == null)
			{
				return null;
			}
			MemoryStream stream2 = FaceBookMgr.getStream(stream);
			if (stream2 == null)
			{
				return null;
			}
			if (!FaceBookMgr.mapHeadImg.ContainsKey(facebookID))
			{
				FaceBookMgr.listFaceBookID.Add(facebookID);
				FaceBookMgr.mapHeadImg.set_Item(facebookID, stream2);
			}
			else
			{
				FaceBookMgr.mapHeadImg.set_Item(facebookID, stream2);
			}
			if (FaceBookMgr.listFaceBookID.get_Count() >= 300)
			{
				List<ulong> list = new List<ulong>();
				list.AddRange(Enumerable.Take<ulong>(FaceBookMgr.listFaceBookID, 100));
				FaceBookMgr.listFaceBookID.RemoveRange(0, 100);
				using (List<ulong>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ulong current = enumerator.get_Current();
						FaceBookMgr.mapHeadImg.Remove(current);
					}
				}
			}
			BitmapImage bitmapImage = null;
			try
			{
				bitmapImage = new BitmapImage();
				bitmapImage.SetSource(stream2);
			}
			catch (Exception ex)
			{
				ex.ToString();
				bitmapImage = null;
			}
			return bitmapImage;
		}
		public static void SetIsFacebookAccountChanged(bool isTrue)
		{
			GConfigMgr.setBool("IsFacebookAccountChanged", isTrue);
		}
		public static bool GetIsFacebookAccountChanged()
		{
			return GConfigMgr.getBool("IsFacebookAccountChanged", false);
		}
	}
}
